(function() {
  'use strict';
  angular.module('services.dataQualityMng')
    .factory('businessForecastingFactory', businessForecastingFactory);

  function businessForecastingFactory($http) {
    return {
      query: query,
      getArrayData: getArrayData
    };

    function query(callback) {
      $http.get('/sas/dataManager/dataQuality/dataApplication/businessForecasting/query')
        .then(callback);
    }

    function getArrayData(data) {
      var echartsOption = {
        xData: [],
        legendName: [],
        seriesData: []
      };
      angular.forEach(data, function(d) {
        echartsOption.xData.push(d.time);
        angular.forEach(d.lineData, function(dd) {
          if (echartsOption.legendName.indexOf(dd.businessName) === -1) {
            echartsOption.legendName.push(dd.businessName);
          }
        });
      });
      var obj = {};
      angular.forEach(data, function(d) {
        angular.forEach(d.lineData, function(bd) {
          if (!obj[bd.businessName]) {
            obj[bd.businessName] = [bd.volume];
          } else {
            obj[bd.businessName].push(bd.volume);
          }
        });
      });
      angular.forEach(echartsOption.legendName, function(ln, index) {
        echartsOption.seriesData[index] = {
          name: ln,
          type: 'line',
          data: obj[ln],
          markPoint: {
            data: []
          }
        };
      });
      return getOption(echartsOption);
    }
    //折线图实例
    function getOption(temp) {
      return {
        title: {
          x: 'center',
          text: '未来一周各业务线交易业务量趋势预测',
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: temp.legendName,
          orient: 'vertical',
          x: 'right',
          y: 'center'
        },
        grid: {
          x2: 130
        },
        calculable: true,
        xAxis: [{
          type: 'category',
          data: temp.xData
        }],
        yAxis: [{
          name: '单位:(笔)',
          type: 'value'
        }],
        series: temp.seriesData
      };
    }
  }
})();
